/***************************************************************************
 *                                                                         *
 * Copyright (c) 2007 - 2009 Nuvoton Technology Corp. All rights reserved.*
 *                                                                         *
 ***************************************************************************/
 

#ifdef ECOS
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "drv_api.h"
#include "diag.h"
#include "wbio.h"
#include "NUC900_GE.h"
#include "NUC900_reg.h"
#else
#include <stdio.h>
#include "NUC900_GE.h"
#include "wbio.h"
#ifdef _NUC900_
#include "NUC900_reg.h"
#else
#include "reg702.h"
#endif
#endif


///////////////////////////
//  rotation function    //
//  Description : Captured the specified photo data from display memory, then  //
//                displayed on display memory by rotation angle  //
//////////////////////////

void rotation(int srcx, int srcy, int destx, int desty, int width, int height, int ctl)
{
	UINT32 cmd32, dest_start, src_start, dimension, pitch;  	
	void *tmpscreen,*orig_dest_start00,*orig_src_start00;
	unsigned int *tmp;
	
	tmpscreen = (void *)malloc(width*height*GFX_BPP/8);
	
#ifdef DEBUG 
	printf("rotation_image()\n");
    printf("(%d,%d)=>(%d,%d)\n", srcx, srcy, destx, desty);
    printf("width=%d height=%d\n", width, height);    
#endif

	memset(tmpscreen,0,width*height*GFX_BPP/8);

  	orig_dest_start00 = (void *)inpw(REG_2D_GEXYDORG); 
  	//orig_src_start00 = (void *)inpw(REG_2D_GEXYSORG);
  	outpw(REG_2D_GEXYDORG, (int)tmpscreen);   //captured photo to another position
  	outpw(REG_2D_GEXYSORG, (int)GFX_START_ADDR);
  	
  	//printf("orig_dest_start00 is %x\n", (int)orig_dest_start00);
  	//printf("orig_src_start00 is %x\n", (int)orig_src_start00);
  	
    //screen_to_screen_blt(srcx,srcy,0,150,width,height);
    source_to_destination_blt(srcx,srcy,0,0,width,height,GFX_WIDTH,width);
    
	/*************************************/
    
  	src_start = dest_start = dimension = cmd32 = pitch = 0;
  	
  	outpw(REG_2D_GEXYDORG, (int)orig_dest_start00);
  	outpw(REG_2D_GEXYSORG, (int)tmpscreen);
  	
  	pitch = GFX_WIDTH << 16 | width;
    outpw(REG_2D_GESDP, pitch);

  	src_start = 0 << 16 | 0;  // captured photo at (0,0) position
 	outpw(REG_2D_GESSXYL, src_start);
  	
 	dest_start = desty << 16 | destx;
 	outpw(REG_2D_GEDSXYL, dest_start);
	  
	dimension = height << 16 | width;
	outpw(REG_2D_GEDIXYL, dimension); 

	cmd32 = 0xcc030000 | (ctl << 1); 
	
	if (_ClipEnable)
  	{
    	cmd32 |= 0x00000200;
    	if (_OutsideClip)
    	{
      		cmd32 |= 0x00000100;
    	}
    	outpw(REG_2D_GEC, cmd32);
	    outpw(REG_2D_GECBTL, _ClipTL);
    	outpw(REG_2D_GECBBR, _ClipBR);
  	}

	/* set rotation reference point xy register, then nothing happened */

	outpw(REG_2D_GEC, cmd32); 

	outpw(REG_2D_GETG, 1); 
  
    while ((inpw(REG_2D_GEINTS)&0x01)==0); // wait for command complete  
  
    outpw(REG_2D_GEINTS, 1); // clear interrupt status 
    
    free(tmpscreen);
} /* end rotation */


/*
///////////////////////////
//  rotation function    //
//  Description : rotation 
//////////////////////////
void rotation(int srcx, int srcy, int destx, int desty, int width, int height, int ctl)
{
	UINT32 cmd32, dest_start, src_start, dimension, pitch;  	

	printf("rotation_image()\n");
    printf("(%d,%d)=>(%d,%d)\n", srcx, srcy, destx, desty);
    printf("width=%d height=%d\n", width, height);    
	
    pitch = GFX_WIDTH << 16 | GFX_WIDTH; // pitch in pixel
    outpw(REG_2D_GESDP, pitch);
    
	src_start = srcy << 16 | srcx;
 	outpw(REG_2D_GESSXYL, src_start);
  
 	dest_start = desty << 16 | destx;
 	outpw(REG_2D_GEDSXYL, dest_start);
	  
	dimension = height << 16 | width;
	outpw(REG_2D_GEDIXYL, dimension); 

	cmd32 = 0xcc430000 | (ctl << 1); 
	
	//
	//outpw(REG_2D_GERRXY, desty<<16 | destx);
	//
	
	outpw(REG_2D_GEC, cmd32); 

	outpw(REG_2D_GETG, 1); 
  
    while ((inpw(REG_2D_GEINTS)&0x01)==0); // wait for command complete  
  
    outpw(REG_2D_GEINTS, 1); // clear interrupt status 
} //end rotation 

*/